package com.jgraves.WikiToSpeech;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

/** Choose a .txt file (or other specified suffix) in the WikiToSpeech directory
 * Passing back result in EXTRA_FILENAME
 * @author johngraves
 * Modified from http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/
 */
public class FileChooser extends ListActivity {

	private FileChooserArrayAdapter adapter;
	private String mDirectoryPreference;
	private int suffixType;
	private String suffix;

	// Define underscore as function to fetch string resources
	private String _(Integer resourceId) {
		return this.getResources().getString(resourceId);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Get directory from preferences (so it can be set once)
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
		mDirectoryPreference = prefs.getString("directory", _(R.string.filepath));
		
		// Get type of file (TXT or ZIP) from intent
		suffixType = getIntent().getIntExtra(Constants.EXTRA_INPUT_TYPE, Constants.TXT);
		if(suffixType==Constants.ZIP) {
			suffix = ".zip";
		} else  {
			suffix = ".txt";
		}
		fillFileChooserArrayAdapter(new File(mDirectoryPreference), suffix);
	}

	private void fillFileChooserArrayAdapter(File directory, String suffix)
	{
		File[] fileArray = directory.listFiles();
		if(null==fileArray){
			Toast.makeText(this, "Directory not found. Check setting.", Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		if(0==fileArray.length){
			Toast.makeText(this, "No files found. Check directory setting or add files.", Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		this.setTitle(_(R.string.directory) + " " + directory.getName());
		List<FileChooserOption> fileList = new ArrayList<FileChooserOption>();
		for(File file: fileArray)
		{
			// Only show files with specified suffix
			if (file.getName().endsWith(suffix)) {
				fileList.add(new FileChooserOption(file.getName(),
						file.length()));
			}
		}
		Collections.sort(fileList);
		adapter = new FileChooserArrayAdapter(FileChooser.this,R.layout.filechooser_view,fileList);
		this.setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		// Pass filename from selected list item into shared preferences
		FileChooserOption selectedListItem = adapter.getItem(position);
		Intent i = new Intent();
		i.putExtra(Constants.EXTRA_FILENAME, mDirectoryPreference + "/" + selectedListItem.getName());
		i.putExtra(Constants.EXTRA_INPUT_TYPE,suffixType);
		setResult(RESULT_OK, i);
		finish();
	}
}